﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Threading;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;
using University.Workshop.Business;
using University.Workshop.DataAccess;

namespace University.Workshop.ClientApp.PokerGame.Transaction
{
    /// <summary>
    /// Interaction logic for PokerGameSendCredit.xaml
    /// </summary>
    public partial class PokerGameSendCredit : Window
    {
        private string LanguageSelected;

        private string TextBlockMessagesInsertTheUserNameToSendCredit;
        private string TextBlockMessagesTheUserNameToSendHaveCharacterNotValid;
        private string TextBlockMessagesTheAmountToSendIsANumber;
        private string TextBlockMessagesTheAmountIsInsufficient;
        private string TextBlockMessagesTheAmountIsZeroOrNegative;
        private string TextBlockMessagesTheUserInsertedIsTheSameOfTheUserName;
        private string TextBlockMessagesTransactionSuccessful;
        private string TextBlockMessagesTransactionFailed;
        private string TextBlockMessagesCAPTCHACorrect;
        private string MessageBoxConfirmCloseTitle;
        private string MessageBoxConfirmCloseQuestion;
        private string TextBlockMessagesTheUserNameToSendDontExist;

        private long AmountActual;
        private long AmountToSend;
        private User FromUserName;
        private string ToUserName;

        TransactionSendManager ManagerTransaction;
        PokerGameConsultingHistory ConsultHistory=null;
        public PokerGameSendCredit()
        {
            InitializeComponent();

            //Client connection
            try
            {
                this.ManagerTransaction = new TransactionSendManager();
                this.ManagerTransaction.TransactionSendManagerConnectClient();
            }
            catch (Exception)
            {
                MessageBox.Show(this.TextBlockMessagesTransactionFailed, ":-(");
                this.Close();
            }
        }

        public PokerGameSendCredit(string Language)
        {
            InitializeComponent();
            
            this.LanguageSelected = Language;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();

            //Client connection
            try
            {
                this.ManagerTransaction = new TransactionSendManager();
                this.ManagerTransaction.TransactionSendManagerConnectClient();
            }
            catch (Exception)
            {
                MessageBox.Show(this.TextBlockMessagesTransactionFailed, ":-(");
                this.Close();
            }          
        }


        public PokerGameSendCredit(string Language, User UserName, long AmountActual)
        {
            InitializeComponent();

            this.LanguageSelected = Language;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();

            //Client connection
            try
            {
                this.ManagerTransaction = new TransactionSendManager();
                this.ManagerTransaction.TransactionSendManagerConnectClient();
            }
            catch (Exception)
            {
                MessageBox.Show(this.TextBlockMessagesTransactionFailed, ":-(");
                this.Close();
            }

            this.FromUserName = UserName;
            this.AmountActual = AmountActual;
            this.LabelHi.Content = this.LabelHi.Content + this.FromUserName.Name;
            this.LabelYourActualAmount.Content = this.LabelYourActualAmount.Content + this.AmountActual.ToString();
            this.LabelThenAmount.Content = this.AmountActual;           
        }


        private void TextBoxNameUserToSendTextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.TextBoxNameUserToSend.Text.Length != 0)
            {
                this.ToUserName = this.TextBoxNameUserToSend.Text.ToString();

                this.TextBoxCreditToSend.IsEnabled = true;

                if (this.TextBoxCreditToSend.Text.Length != 0)
                {
                    this.ButtonSend.IsEnabled = true;
                }
            }
            else
            {
                this.TextBoxCreditToSend.IsEnabled = false;                
                this.ButtonSend.IsEnabled = false;
            }
        }

        private void TextBoxCreditToSendTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                this.AmountToSend = Convert.ToInt64(this.TextBoxCreditToSend.Text.ToString());

                if (this.AmountToSend > this.AmountActual)
                {
                    MessageBox.Show(this.TextBlockMessagesTheAmountIsInsufficient, ":-O");
                    this.TextBlockMessages.Text = this.TextBlockMessagesTheAmountIsInsufficient;
                    this.TextBoxCreditToSend.Text = "";
                }
                else
                {
                    if (this.AmountToSend <= 0)
                    {
                        this.TextBoxCreditToSend.Text = "";
                        this.TextBlockMessages.Text = this.TextBlockMessagesTheAmountIsZeroOrNegative;
                    }
                    else
                    {
                        this.LabelThenAmount.Content = this.AmountActual - this.AmountToSend;
                        this.ButtonSend.IsEnabled = true;
                    }
                }
            }
            catch (Exception)
            {
                if (string.IsNullOrEmpty(this.TextBoxCreditToSend.Text) == false)
                {
                    //Clear the last caracter of the textbox
                    string WordToChange = this.TextBoxCreditToSend.Text;
                    int Pointer = WordToChange.Length - 1;
                    WordToChange = WordToChange.Remove(Pointer);
                    this.TextBoxCreditToSend.Text = WordToChange;
                    this.TextBoxCreditToSend.Select(this.TextBoxCreditToSend.Text.Length, 0);

                    this.TextBlockMessages.Text = TextBlockMessagesTheAmountToSendIsANumber;
                    //this.LabelThenAmount.Content = this.AmountActual;

                    if (string.IsNullOrEmpty(this.TextBoxCreditToSend.Text))
                    {
                        this.ButtonSend.IsEnabled = false;
                    }
                }
                else
                {
                    this.ButtonSend.IsEnabled = false;
                }
            }
        }


        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ButtonSendClick(object sender, RoutedEventArgs e)
        {            
            int ResultManager = ManagerTransaction.SendCredit(this.FromUserName.Name, this.TextBoxNameUserToSend.Text.ToString(), this.AmountToSend, (this.AmountActual - this.AmountToSend));

            switch (ResultManager)
            {
                case 0:
                    {
                        //Here the sentences if everything is correct or not                
                        //If is correct, the programa show a message of succesfull
                        MessageBox.Show(this.TextBlockMessagesTransactionSuccessful, ":-)");
                        this.FromUserName.Chips = this.AmountActual - this.AmountToSend;
                        this.CloseConfirmation();
                    }break;

                case 1:
                    {
                        MessageBox.Show(this.TextBlockMessagesTheUserInsertedIsTheSameOfTheUserName, ":-|");

                        this.TextBlockMessages.Text = this.TextBlockMessagesTheUserInsertedIsTheSameOfTheUserName;

                        this.TextBoxCreditToSend.IsEnabled = false;                        
                        this.ButtonSend.IsEnabled = false;

                        this.TextBoxNameUserToSend.Focus();
                        this.TextBoxNameUserToSend.SelectAll();
                    }break;

                case 2:
                    {
                        MessageBox.Show(this.TextBlockMessagesTheUserNameToSendHaveCharacterNotValid, ":-O");
                        this.TextBlockMessages.Text = this.TextBlockMessagesTheUserNameToSendHaveCharacterNotValid;

                        this.TextBoxCreditToSend.IsEnabled = false;
                        this.ButtonSend.IsEnabled = false;

                        //The textbox of user name will be selected
                        this.TextBoxNameUserToSend.Focus();
                        this.TextBoxNameUserToSend.SelectAll();
                    }break;

                case 3:
                    {
                        //If is fail, the programa show a message of failed
                        MessageBox.Show(this.TextBlockMessagesTransactionFailed, ":-(");
                        this.AmountToSend = 0;
                        this.CloseConfirmation();                        
                    }break;

                case 4:
                    {
                        MessageBox.Show(this.TextBlockMessagesTheUserNameToSendDontExist, ":-O");
                        this.TextBlockMessages.Text = this.TextBlockMessagesTheUserNameToSendHaveCharacterNotValid;

                        this.TextBoxCreditToSend.IsEnabled = false;
                        this.ButtonSend.IsEnabled = false;

                        //The textbox of user name will be selected
                        this.TextBoxNameUserToSend.Focus();
                        this.TextBoxNameUserToSend.SelectAll();
                    } break;
            }                                   
        }

        private void CloseConfirmation()
        {
            //The program send a question to close or they where doing more transaction
            MessageBoxResult Result = MessageBox.Show(this.MessageBoxConfirmCloseQuestion, this.MessageBoxConfirmCloseTitle, MessageBoxButton.YesNo);
            if (Result == MessageBoxResult.No)
            {
                this.Close();
            }
            else
            {
                this.RestoreValuesAndLabels();
            }
        }

        private void RestoreValuesAndLabels()
        {
            string AuxiliarLabelYourActualAmount = this.LabelYourActualAmount.Content.ToString();
            string DeleteTheAmount = Convert.ToString(this.AmountActual);
            AuxiliarLabelYourActualAmount = AuxiliarLabelYourActualAmount.Replace(DeleteTheAmount, "");


            //Restore the new value from the transaction
            this.AmountActual = this.AmountActual - this.AmountToSend;
            this.LabelYourActualAmount.Content = AuxiliarLabelYourActualAmount + this.AmountActual.ToString();
            this.LabelThenAmount.Content = this.AmountActual;

            
            this.TextBoxNameUserToSend.Text = "";

            this.TextBoxCreditToSend.Text = "";
            this.TextBoxCreditToSend.IsEnabled = false;
        }

        private void HyperlinkConsultHistoryClick(object sender, RoutedEventArgs e)
        {
            ConsultHistory = new PokerGameConsultingHistory(this.LanguageSelected, this.FromUserName.Name);
            ConsultHistory.ShowDialog();
        }

        private void ApplyLanguage()
        {
            //Labels content
            this.Title = ClientLanguagePack.PokerGameSendCreditTitle;
            this.LabelTitle.Content = ClientLanguagePack.PokerGameSendCreditTitle;
            this.LabelHi.Content = ClientLanguagePack.PokerGameSendCreditLabelHi;            
            this.LabelInsertTheAmount.Content = ClientLanguagePack.PokerGameSendCreditLabelInsertTheAmount;
            this.LabelSendUserName.Content = ClientLanguagePack.PokerGameSendCreditLabelSendUserName;
            this.LabelThenYourAmountWillBe.Content = ClientLanguagePack.PokerGameSendCreditLabelTheYourAmountWillBe;
            this.LabelYourActualAmount.Content = ClientLanguagePack.PokerGameSendCreditLabelYourActualAmount;

            //Button content
            this.ButtonCancel.Content = ClientLanguagePack.PokerGameSendCreditButtonCancel;
            this.ButtonSend.Content = ClientLanguagePack.PokerGameSendCreditButtonSend;

            //Hyperlink content
            this.HyperLinkConsultHistory.Text = ClientLanguagePack.PokerGameSendCreditHyperLinkConsultHistory;

            //Error and success messages possible
            this.TextBlockMessagesInsertTheUserNameToSendCredit = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesInsertTheUserNameToSendCredit;
            this.TextBlockMessagesTheAmountToSendIsANumber = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTheAmountToSendIsANumber;
            this.TextBlockMessagesTheAmountIsInsufficient = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTheAmountIsInsufficient;
            this.TextBlockMessagesTheAmountIsZeroOrNegative = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTheAmountIsZeroOrNegative;
            this.TextBlockMessagesTheUserInsertedIsTheSameOfTheUserName = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTheUserInsertedIsTheSameOfTheUserName;
            this.TextBlockMessagesTheUserNameToSendHaveCharacterNotValid = ClientLanguagePack.PokerGameSendcreditTextBlockMessagesTheUserNameToSendHaveCharacterNotValid;
            this.TextBlockMessagesTransactionSuccessful = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTransactionSuccessful;
            this.TextBlockMessagesTransactionFailed = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTransactionFailed;
            this.TextBlockMessagesCAPTCHACorrect = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesCAPTCHACorrect;
            this.TextBlockMessagesTheUserNameToSendDontExist = ClientLanguagePack.PokerGameSendCreditTextBlockMessagesTheUserNameToSendDontExist;
            this.MessageBoxConfirmCloseQuestion = ClientLanguagePack.PokerGameSendCreditMessageBoxConfirmCloseQuestion;
            this.MessageBoxConfirmCloseTitle = ClientLanguagePack.PokerGameSendCreditMessageBoxConfirmCloseTitle;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (ConsultHistory != null)
            {
                if(ConsultHistory.IsLoaded)
                ConsultHistory.Close();
            }
        }
    }
}
